//
//  HZPeiziDealVC.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/11/29.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziDealVC.h"
#import "HZPeiziQueryView.h"
#import "MYHttp.h"

@interface HZPeiziDealVC ()
@property (nonatomic, strong)HZPeiziQueryView *pQueryView;
@property (nonatomic, strong)NSMutableArray *pQueryArray;
@end

@implementation HZPeiziDealVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"成交查询";
    
    _pQueryArray = [NSMutableArray new];
    
    //关联逻辑管理对象
    self.logicManager = [HZBaseLogjcManager new];
    self.view.logicManager = self.logicManager;
    
    _pQueryView = [HZPeiziQueryView new];
    _pQueryView.nQueryType = kQuery_deal_Type;
    [self.view addSubview:_pQueryView];
    [_pQueryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [self p_registerBlock];
}

#pragma mark -
#pragma mark blockAction
- (void)p_registerBlock
{
    __weak typeof (self)weakSelf = self;
    [self.logicManager mapLogic:kLogicRouterPeiziDealFind toBlock:^id(NSDictionary *params, id other) {
        if([other isKindOfClass:[NSArray class]]){
            NSArray *pTempArray = (NSArray *)other;
            if(pTempArray.count == 3){
                NSString *startDate = pTempArray[0];
                NSString *endDate = pTempArray[1];
                NSString *showType = pTempArray[2];
                
                if([showType isEqualToString:@""]){
                    [weakSelf p_sendExcutingRequestWithStartDate:startDate endDate:endDate];
                }else if([showType isEqualToString:@"1"]){
                    [weakSelf filterQueryData:@"买入"];
                }else if([showType isEqualToString:@"2"]){
                    [weakSelf filterQueryData:@"卖出"];
                }
            }
        }
        return nil;
    }];
}

- (void)filterQueryData:(NSString *)showType{
    if(_pQueryArray.count > 0){
        NSMutableArray *tempArray = [NSMutableArray array];
        for(int i=0;i<_pQueryArray.count;i++){
            HZPeiziCancelOrderListModel *model = (HZPeiziCancelOrderListModel *)_pQueryArray[i];
            if([model.bstype isEqualToString:showType]){
                [tempArray addObject:model];
            }else if([model.bstype isEqualToString:showType]){
                [tempArray addObject:model];
            }
        }
        [_pQueryView updateNewData:tempArray];
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self sendRequest];
}

- (void)sendRequest
{
    NSString *startDate = @"";
    NSString *endDate = @"";
    [self p_sendExcutingRequestWithStartDate:startDate endDate:endDate];
}

#pragma mark -
#pragma mark request
- (void)p_sendExcutingRequestWithStartDate:(NSString *)startDate
                                   endDate:(NSString *)endDate
{
    [SVProgressHUD show];
    NSArray *array = @[@{@"funcid":@"3",@"startdate":startDate,@"enddate":endDate,@"session":[HZUserManager manager].peiziAccountSession}];
    [MYHttp post:kAPIPeiziHost params:array success:^(id response) {
        [_pQueryArray removeAllObjects];
//        NSMutableArray *tempArray = [NSMutableArray array];
        if([response isKindOfClass:[NSArray class]]){
            for (NSDictionary *dict in response) {
                HZPeiziChengjiaoListModel *model = [HZPeiziChengjiaoListModel mj_objectWithKeyValues:dict];
                [_pQueryArray addObject:model];
            }
        }
        [_pQueryView updateNewData:_pQueryArray];
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}
@end
